package com.codeoss.framework.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.codeoss.framework.response.ImageResponse;
import com.codeoss.framework.service.IImageService;

@Controller
@RequestMapping("image")
public class ImageController {
	@Autowired
	private IImageService imageService;
	
	@RequestMapping(value = "upload", method = RequestMethod.POST)
	@ResponseBody
	public ImageResponse upload(@RequestParam("file") MultipartFile file){
		ImageResponse imageResponse=imageService.upload(file);
		return imageResponse;
	}
	
	@RequestMapping(value = "getImageByFilename", method = RequestMethod.GET)
	public void getImageByFilename(@RequestParam("filename") String filename,HttpServletResponse response)throws Exception{
		ImageResponse imageResponse=imageService.getImageByFilename(filename);
		response.getOutputStream().write(imageResponse.getContent());
	}
	
	@RequestMapping(value = "getImageByField", method = RequestMethod.GET)
	public void getImageByField(@RequestParam("model") String model,@RequestParam("dataField") String dataField,@RequestParam("id") long id,HttpServletResponse response)throws Exception{
		ImageResponse imageResponse=imageService.getImageByField(id, model, dataField);
		if(imageResponse.getContent()!=null) {
			response.getOutputStream().write(imageResponse.getContent());
		}	
	}
}
